package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;

import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
   @Autowired
    private BrandService brandService;
    //根据品牌id集合查询品牌集合
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryByBrandId(@RequestParam("ids")List<Long> ids){
            return ResponseEntity.ok(this.brandService.queryByBrandId(ids));
        }




    //根据分类id查询品牌，失败,解决
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id")Long id){
            return ResponseEntity.ok(this.brandService.queryByCategoryId(id));
    }





    //根据id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandByBrandId(@PathVariable("id") Long id){
            return ResponseEntity.ok(this.brandService.queryBrandByBrandId(id));
    }


    //分页查询品牌
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                       @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                       @RequestParam(value = "key",required = false)String key){
        return ResponseEntity.ok(this.brandService.pageQuery(page,rows,key));
    }

    //新增品牌,失败
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO){
       this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(201).build();
    }

    //更新品牌
    @PutMapping
    public ResponseEntity<Void> upDateBrandById(BrandDTO brandDTO){
        this.brandService.upDateBrandById(brandDTO);
        return ResponseEntity.status(204).build();
    }


    //根据id删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id")Long id){
        this.brandService.deleteBrandById(id);
        return ResponseEntity.ok().build();
    }

}
